package com.zhuiyun.pm.sys.menu.service.impl;

import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.sys.menu.entity.MenuEntity;
import com.zhuiyun.pm.sys.menu.mapper.MenuMapper;
import com.zhuiyun.pm.sys.menu.service.MenuService;
import com.zhuiyun.pm.sys.role.entity.SysRoleEntity;
import com.zhuiyun.pm.sys.role.mapper.SysRoleMapper;
import com.zhuiyun.pm.sys.user.entity.SysUserEntity;
import com.zhuiyun.pm.sys.user.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜单管理
 * @Author lx
 * @Date 2023/3/13 9:58
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Autowired
    SysUserMapper userMapper;

    @Autowired
    SysRoleMapper roleMapper;

    /**
    * @Author lx
    * @Description 显示菜单信息
    * @Date 2023/3/20 11:08
    * @Return java.util.List<com.zhuiyun.pm.sys.menu.entity.MenuEntity>
    */
    @Override
    public List<MenuEntity> getByMenu() {
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setMenuDelFlag(0);
        List<MenuEntity> typeTrees = menuMapper.select(menuEntity);
        List<MenuEntity> newTypeTree = new ArrayList<>();
        for(MenuEntity types: typeTrees){
            if(types.getMenuParentId()==0){
                newTypeTree.add(getChild(types,typeTrees));
            }
        }
        return newTypeTree;
    }

    /**
    * @Author lx
    * @Description 模糊查询
    * @Date 2023/3/20 13:05
    * @Return java.util.List<com.zhuiyun.pm.sys.menu.entity.MenuEntity>
    * @param menuName
    */
    @Override
    public List<MenuEntity> getAllMenuByName(String menuName) {
        Example example = new Example(MenuEntity.class);
        example.createCriteria().andLike("menuName","%"+menuName+"%").andEqualTo("menuDelFlag",0);
        List<MenuEntity> typeTrees = menuMapper.selectByExample(example);
        List<MenuEntity> newTypeTree = new ArrayList<>();
        for(MenuEntity types: typeTrees){
            if(types.getMenuParentId()==0){
                newTypeTree.add(getChild(types,typeTrees));
            }
        }
        return newTypeTree;
    }



    /**
    * @Author lx
    * @Description 递归
    * @Date 2023/3/13 15:34
    * @Return com.zhuiyun.pm.sys.menu.entity.MenuEntity
    * @param menuEntity
* @param menuEntityList
    */
    private MenuEntity getChild(MenuEntity menuEntity, List<MenuEntity> menuEntityList) {
        ArrayList<MenuEntity> newMenu = new ArrayList<>();
        for (MenuEntity entity : menuEntityList) {
            if(entity != null){
                if(menuEntity.getId().equals(entity.getMenuParentId())){
                    newMenu.add(getChild(entity,menuEntityList));
                }
            }
        }
        menuEntity.setChilds(newMenu);
        return menuEntity;
    }


    /**
    * @Author lx
    * @Description 新增菜单
    * @Date 2023/3/13 15:34
    * @Return int
    * @param menuEntity
    */
    @Override
    public int saveMenu(MenuEntity menuEntity) {
        menuEntity.setMenuCreateTime(new Date());
        menuEntity.setMenuUpdateTime(new Date());
        menuEntity.setMenuDelFlag(0);
        if (menuEntity.getMenuParentId() == null) {
            menuEntity.setMenuParentId(0);
        }
        return menuMapper.insert(menuEntity);
    }

    /**
    * @Author lx
    * @Description 删除菜单信息
    * @Date 2023/3/13 15:34
    * @Return java.lang.Integer
    * @param id
    */
    @Override
    public Integer deleteMenuById(Integer id) {
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setId(id);
        menuEntity.setMenuDelFlag(1);
        return menuMapper.updateByPrimaryKeySelective(menuEntity);
    }

    /**
    * @Author lx
    * @Description 修改菜单信息
    * @Date 2023/3/13 15:33
    * @Return java.lang.Integer
    * @param menuEntity
    */
    @Override
    public Integer updateMenuById(MenuEntity menuEntity) {
        menuEntity.setMenuUpdateTime(new Date());
        Integer id = menuMapper.updateByPrimaryKeySelective(menuEntity);
        return id;
    }


    /**
    * @Author lx
    * @Description 实现动态路由
    * @Date 2023/3/13 15:38
    * @Return com.zhuiyun.pm.config.CommonResult
    */
    @Override
    public CommonResult getMenuListByPower(Integer id) {
        SysUserEntity user = new SysUserEntity();
        user.setId(id);
        SysUserEntity sysUser = userMapper.selectOne(user);
        SysRoleEntity role = new SysRoleEntity();
        role.setId(sysUser.getRoleId());
        SysRoleEntity roleEntity = roleMapper.selectOne(role);
        String power = roleEntity.getRolePermissions();
        String[] split = power.split(",");
        ArrayList<MenuEntity> menuList = new ArrayList<>();
        for (String s : split) {
            int i = Integer.parseInt(s);
            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setMenuDelFlag(0);
            menuEntity.setId(i);
            MenuEntity select = menuMapper.selectOne(menuEntity);
            menuList.add(select);
        }
        ArrayList<MenuEntity> newMenuList = new ArrayList<>();
        for (MenuEntity menuEntity : menuList) {
            if(menuEntity != null) {
                if(menuEntity.getMenuParentId()==0){
                    newMenuList.add(getChild(menuEntity,menuList));
                }
            }
        }
        return CommonResult.ok(newMenuList);
    } 


}
